package com.core.indexer;

import java.util.HashMap;
import java.util.Map;

public class Accumulator {

	private Map<String, Double> scoreMap;
	private static Accumulator mInstance = null;
	private SearchCore mContext = null;
	
	public static Accumulator Get()
	{
		if(mInstance == null)
			mInstance = new Accumulator();
		
		return mInstance;
	}
	
	public void Init(SearchCore context)
	{
		mContext = context;
	}
	
	public int GetR()
	{
		return scoreMap.size();
	}
	
	public boolean HasDocument(String docNO)
	{
		return scoreMap.containsKey(docNO);
	}
	
	public Map<String, Double> PostProcess()
	{
		for(String key : scoreMap.keySet())
		{
			double score = scoreMap.get(key);
			score = score/mContext.GetDocWeight(key);
			scoreMap.put(key, score);
		}
		return scoreMap;
	}
	
	public void InsertData(String docNo, double score)
	{
		Double value;
		if((value = scoreMap.get(docNo)) == null)
		{
			scoreMap.put(docNo, score);
		}
		else
		{
			value += score;
			scoreMap.put(docNo, value);
		}
	}
	
	private Accumulator()
	{
		scoreMap = new HashMap<String, Double>();
	}
}
